﻿using Fast.ConfigureOptions;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;

namespace Fast.Extensions.Caching
{
    /// <summary>
    /// 分布式缓存选项配置
    /// </summary>
    public class FastDistributedCacheOptions : IConfigureOptions<FastDistributedCacheOptions>
    {
        /// <summary>
        /// 缓存键前缀
        /// </summary>
        public string KeyPrefix { get; set; }

        /// <summary>
        /// 全局缓存选项
        /// </summary>
        public DistributedCacheEntryOptions GlobalCacheEntryOptions { get; set; }

        /// <summary>
        /// 所有缓存配置程序的列表
        /// (func参数：缓存的名称)
        /// </summary>
        public List<Func<string, DistributedCacheEntryOptions>> CacheConfigurators { get; set; }

        public void PostConfigure(FastDistributedCacheOptions options, IConfiguration configuration)
        {
            options.CacheConfigurators ??= new List<Func<string, DistributedCacheEntryOptions>>();
            options.GlobalCacheEntryOptions ??= new DistributedCacheEntryOptions();
            options.GlobalCacheEntryOptions.SlidingExpiration ??= TimeSpan.FromMinutes(20);
            options.KeyPrefix ??= "";
        }
    }
}
